package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC399 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
        * @param 此题目参考了别人代码
        * 这是因为问题情况较为复杂
        * 未来需要再次复习此道题目
        * @return:
        */
    class Solution {
        Map<String, String> parents;
        Map<String, Double> val;

        public String find(String x) {
            if (!x.equals(parents.get(x))) {
                String tmpParent = parents.get(x);
                String root = find(tmpParent);
                double oldVal = val.get(x);
                val.put(x, oldVal * val.get(tmpParent));
                parents.put(x, root);
            }
            return parents.get(x);
        }

        public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
            parents = new HashMap<>();
            val = new HashMap<>();
            int i = 0;
            for (List<String> equation : equations) {
                String from = equation.get(0);
                String to = equation.get(1);
                double cur = values[i];
                if (!parents.containsKey(from) && !parents.containsKey(to)) {
                    parents.put(from, to);
                    val.put(from, cur);
                    parents.put(to, to);
                    val.put(to, 1.0);
                } else if (!parents.containsKey(from)) {
                    parents.put(from, to);
                    val.put(from, cur);
                } else if (!parents.containsKey(to)) {
                    parents.put(to, from);
                    val.put(to, 1 / cur);
                } else {
                    String pa = find(from);
                    String pb = find(to);
                    if (!pa.equals(pb)) {
                        parents.put(pa, pb);
                        val.put(pa, cur * val.get(to) / val.get(from));
                    }
                }
                i++;
            }
            i = 0;
            double[] res = new double[queries.size()];
            for (List<String> query : queries) {
                String from = query.get(0);
                String to = query.get(1);
                if (!parents.containsKey(from) || !parents.containsKey(to)) {
                    res[i++] = -1;
                    continue;
                }
                String pa = find(from);
                String pb = find(to);
                if (!pa.equals(pb)) res[i] = -1;
                else {
                    res[i] = val.get(from) / val.get(to);
                }
                i++;
            }
            return res;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC399 an = new LC399();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
